package ph.greenova.persistence.impl;

import ph.greenova.persistence.dao.PersonDAO;
import ph.greenova.persistence.model.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * Created By: adrian
 * Date Created: 3:46 PM 6/5/13
 */
//@Repository
public class PersonDAOImpl /*extends AbstractDAOImpl<Person>*/ implements PersonDAO {

    private static final TreeMap<Long, Person> persons = new TreeMap<Long, Person>();

    public PersonDAOImpl() {

        if (persons.isEmpty()) {
            Person p = new Person();
            p.setId(1l);
            p.setFirstName("Adrian");
            p.setLastName("Viesca");
            p.setUsername("adrian.viesca@gmail.com");
            p.setPassword("password");

            persons.put(p.getId(), p);

            p = new Person();
            p.setId(2l);
            p.setFirstName("Joseph");
            p.setLastName("Viesca");
            p.setUsername("joseph.viesca@gmail.com");
            p.setPassword("password");

            persons.put(p.getId(), p);
        }
    }

    @Override
    public Person create(final Person entity) {
        entity.setId(Long.valueOf(persons.size() + 1));
        persons.put(entity.getId(), entity);
        return entity;
    }

    @Override
    public Person find(Object id) {
        return persons.get(id);
    }


    @Override
    public List<Person> findAll() {
        return new ArrayList<Person>(persons.values());
    }


    @Override
    public Person findPersonByUserName(final String username) {
        Person per = null;

        for (Person p : persons.values()) {
            if (p.getUsername().equals(username)) {
                per = p;
            }
        }


        return per;
    }
}
